package com.karrim.server.util;

import com.karrim.server.io.def.Tokenizer;
import java.util.Arrays;
import java.util.NoSuchElementException;

/**
 *
 * @author andrea.cantafio
 */
public class TokenizerImpl implements Tokenizer {

    public TokenizerImpl(String str, char token) {
        this.str = str.toCharArray();
        this.token = token;

        start = 0;
    }

    @Override
    public boolean hasNext() {
        for (int i = start; i < str.length; i++) {
            if (str[i] == token) {
                return true;
            }
        }
        return false;
    }

    @Override
    public String next() throws NoSuchElementException {
        for (int i = start; i < str.length; i++) {
            if (str[i] == token) {
                String next = new String(Arrays.copyOfRange(str, start, i));
                start = ++i;
                return next;
            }
        }
        throw new NoSuchElementException();
    }
    private int start;
    private char[] str;
    private char token;
}
